"""
   author: 陈亚敏
   data: 2021/11/11  17:06
   project: 1
"""


user = {'cym': {'passwd': '11223', 'money': 0},#用户表
        'hj': {'passwd': '3333', 'money': 0}}
#密码校对函数
def repasswd(passwd):

    def type(passwd):
        string = "~!@#$%^&*()_+-*/<>,.[]\/"
        s3 = set()
        for i in passwd:
            if i.isdigit():
                s3.add(1)
            elif i.isupper():
                s3.add(2)
            elif i.islower():
                s3.add(3)
            elif (i in string):
                s3.add(4)
        if len(s3) >= 3:
            return passwd
        else:
            return print("密码字符类型不足三类")

    def repeat(passwd):
        s2 = ''
        for j in passwd:
            s2 += j
            if len(s2) >= 3:
                if passwd.count(s2) > 1:
                    return print("有重复字符串！")
                else:
                    return passwd

    if len(passwd) >= 8:
        type(passwd)
        repeat(passwd)
        return passwd

    else:
        return print("密码长度不足八位！")


#注册函数
def signin(username,passwd):
    if username not in user:
        if repasswd(passwd):
            user[username]={'passwd':passwd,'money':0}
            print("注册成功！")
            return user
        else:
            return "密码输入有误！"
    else:
        return "用户已存在"


maneger = {'feng': '12345', 'wen': '333'}#管理员表
lo = {}#空字典，用于存放购买商品信息
#管理员登录函数
def manage(managername,passwd):
    if managername in maneger and passwd == maneger[managername]:
        print(managername,"登录成功！")
        return 1
    else:
        print("管理员用户名密码有误")
#管理员增加货品函数
def addgoods(num,gname,gprice):
    if num not in goods:
        goods[num]={'num':gname,'price':int(gprice)}
        print("添加成功！")
        return goods
    else:
        return print("该编号已存在！请重新输入")

goods = {'1': {'num': '香蕉', 'price': 14},
         '2': {'num': '苹果', 'price': 30},
         '3': {'num': '橘子', 'price': 14},
         '4': {'num': '猕猴桃', 'price':50},
         '5': {'num': '蓝莓', 'price': 25},
         '6': {'num': '山竹', 'price': 48}}#货品表
#用户登录函数
def login(username, passwd):
    if username in user and passwd == user[username]['passwd']:
        return 1
    else:
        print("用户名密码有误")
#用户选购函数
def goodsdef(g_num, q):
    if g_num in goods:
        if user[username]['money'] > int(q) * goods[g_num]['price']:
            user[username]['money'] -= int(q) * goods[g_num]['price']
            if goods[g_num]['num'] in lo:
                lo[goods[g_num]['num']]+=int(q)
            else:
                lo[goods[g_num]['num']] = int(q)
        elif user[username]['money'] < int(q) * goods[g_num]['price']:
            print(f"钱不够了！！你只有{user[username]['money']}元！")
    else:
        print("无此产品！")
#用户预算函数
def money(m):
    user[username]['money'] = m

print("欢迎来到三乐购物系统".center(90,'*'))

while 1:
    choise = input("管理员请按1，新用户注册请按2,顾客登录请按3,退出系统请按4：")
    if choise=='1':
        print("三乐购物系统管理员界面".center(90, "*"))
        a=input("请输入你的管理员用户名：")
        if a=='exit':
            continue
        b=input("请输入你的密码:")
        if manage(a,b)==1:
            while 1:
                num = input("请输入你要添加的物品代码(输入exit退出)：")

                if num == 'exit':
                        break
                gname = input("请输入品名：")
                if gname == 'exit':
                        break
                gprice = input("请输入价格：")
                if gprice== 'exit':
                    break
                addgoods(num,gname,gprice)
            print(goods)
    elif choise == '2':
        print("三乐购物系统注册界面".center(90, "*"))
        username = input("请输入用户名：exit退出")
        if username == 'exit':
            print("886")
        else:
            passwd = input("请输入密码：")

            signin(username, passwd)
    elif choise=='3':
        print("三乐购物用户界面".center(90, "*"))
        username = input("please input your username:")
        passwd = input("please input your passwd:")
        if login(username, passwd):
            print(username, '欢迎!祝您购物愉快！有意见或建议请联系陈经理：15399998888')
            print(goods)
            money(int(input("告诉我你的预算吧：")))
            while 1:
                g_num = input("请输入你要购买的物品代码(输入exit退出，输入car查看已购买信息)：")
                if g_num == 'exit':
                    break
                elif g_num=='car':
                    for i, j in lo.items():
                        print("购物车情况如下：")
                        print(f"{j}个{i}")
                    continue

                q = input("请输入你要购买的的数量：")
                if q == 'exit':
                    break

                goodsdef(g_num, q)
        for i,j in lo.items():
             print(f"你买了{j}个{i}")
             print(f"余额{user[username]['money'] }元")
    elif choise=='4':
        break

print("谢谢光临！有缘再会！")